package mover;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.*;

import xmlParser.XmlParser;

import extensible.Canvas;
import extensible.Factory;



/**
 * Creates a number of masses and springs, whose attributes are read from the given data file.
 */
public class SpringFactory extends Factory
{
    /**
     * Construct factory.
     */
    public SpringFactory ()
    {
        super("Create Springs");
    }


    /**
     * Creates a number of bouncing balls within the given canvas using data from the given file.
     *
     * @param canvas within which shapes should appear
     * @param input scanner attached to a file with the appropriate format
     */
	public void createMovers(Canvas canvas, int number, int trailSize,
			int distanceFrom, int proximityTo) {
		Dimension bounds = canvas.getSize();
		int size = 50;
		for (int i = 0; i < number; i++) {
			List<Mass> masses = new ArrayList<Mass>();
        	// create an initial scenario to animate
        	masses.add(new Mass(new Point2D.Double(canvas.nextIntInRange(size /2, bounds.width - size / 2), canvas.nextIntInRange(size /2, bounds.height - size / 2)), new Dimension(size, size), Color.GREEN, 5, trailSize));
        	masses.add(new Mass(new Point2D.Double(canvas.nextIntInRange(size /2, bounds.width - size / 2), canvas.nextIntInRange(size /2, bounds.height - size / 2)), new Dimension(50, 50), Color.RED, 7, trailSize));
        	masses.add(new Mass(new Point2D.Double(canvas.nextIntInRange(size /2, bounds.width - size / 2), canvas.nextIntInRange(size /2, bounds.height - size / 2)), new Dimension(50, 50), Color.MAGENTA, 10, trailSize));
        	canvas.add(new Spring(masses.get(0), masses.get(1), 150, 0.005));
        	canvas.add(new Spring(masses.get(0), masses.get(2), 100, 0.005));
        	canvas.add(new Spring(masses.get(1), masses.get(2), 100, 0.005));

        	for (Mass m : masses)
        	{
        		canvas.add(m);
        	}
		}
	}
	
	/**
	 * Creates movers from a canvas and a specified XML parser
	 */
	public void createMovers(Canvas canvas, XmlParser parser) {
		List<Mass> masses = parser.getNodesList();
		List<Spring> springs = parser.getSpringsList();
		for (Spring s : springs) {
			System.out.println("Spring is"+s);
			canvas.add(s);
		}
		for (Mass m : masses) {
			System.out.println("Mass is"+m);
			canvas.add(m);
		}
	}
	
}
